package com.easylinkin.linkappapi.device.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.common.utils.DateUtil;
import com.easylinkin.linkappapi.device.constant.DeviceConstant.GrainSize;
import com.easylinkin.linkappapi.device.entity.ElectricyRecords;
import com.easylinkin.linkappapi.device.entity.vo.ElectricyRecordsVo;
import com.easylinkin.linkappapi.device.entity.vo.ElectricyStatisticsVo;
import com.easylinkin.linkappapi.device.service.ElectricyRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.time.Year;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 用电历史记录 前端控制器
 * </p>
 *
 * @author Xiaoyang
 * @since 2022-08-03
 */
@RestController
@Api(value="用电历史记录控制器",tags={"用电历史记录控制器"})
@RequestMapping("electricyRecords")
public class ElectricyRecordsController extends ApiController {

  /**
   * 服务对象
   */
  @Resource
  private ElectricyRecordsService electricyRecordsService;

  /**
   * 查询用电统计
   * @param electricyRecordsVo 实体对象
   * @return
   */
  @PostMapping("getElectricyStatistics")
  @ApiOperation("查询用电统计")
  public RestMessage getElectricyStatistics(@RequestBody ElectricyRecordsVo electricyRecordsVo) {
//    Assert.notNull(electricyRecordsVo.getDeviceCode(), "设备号 不能为空");
    return RestBuilders.successBuilder()
        .data(electricyRecordsService.getElectricyStatistics(electricyRecordsVo)).build();
  }

  /**
   * 用电趋势图
   * @param electricyRecordsVo
   * @return
   */
  @PostMapping("countElectricyStatistics")
  @ApiOperation("用电趋势图")
  public RestMessage countElectricyStatistics(@RequestBody ElectricyRecordsVo electricyRecordsVo){
    Assert.notNull(electricyRecordsVo, "electricyRecordsVo 不能为空");
    Assert.notNull(electricyRecordsVo.getStartTime(), "开始时间 不能为空");
    Assert.notNull(electricyRecordsVo.getEndTime(), "结束时间 不能为空");
    Assert.notNull(electricyRecordsVo.getGrainSize(), "粒度 不能为空");
//    Assert.notNull(electricyRecordsVo.getDeviceCode(), "设备号 不能为空");
    DateUtil.checkDate(electricyRecordsVo.getGrainSize(), electricyRecordsVo.getStartTime(),
        electricyRecordsVo.getEndTime());
    List<ElectricyStatisticsVo> record = electricyRecordsService.getElectricyStatisticsList(electricyRecordsVo);
    return RestBuilders.successBuilder(record).build();
  }

  /**
   *
    * @param electricyRecordsVo
   * {
   *     year: //年份
   *     deviceCode: //电表设备code
   *     sortField: //总用电量汇总统计  排序条件 只能是year或者sum 默认 year
   *     sortMethod: //总用电量汇总统计  排序条件 0降序,1升序 默认0
   * }
   * @return
   */
  @PostMapping(value = "/electricyByYear")
  public RestMessage electricyByYear(@RequestBody ElectricyRecordsVo electricyRecordsVo){
     if (StringUtils.isEmpty(electricyRecordsVo.getYear())){
        //设置默认年份
        electricyRecordsVo.setYear(String.valueOf(Year.now().getValue()));
     }
     //排序字段初始化
     if (StringUtils.isEmpty(electricyRecordsVo.getSortField())){
        electricyRecordsVo.setSortField("year");
     }
     if (Objects.isNull(electricyRecordsVo.getSortMethod())){
       electricyRecordsVo.setSortMethod(0);
     }
     List<Map<String,Object>> result = electricyRecordsService.electricyByYear(electricyRecordsVo);
     return RestBuilders.successBuilder(result).build();
  }

    /**
     * 获取最早记录
     * @return
     */
    @GetMapping(value = "/getStartRecord")
    public RestMessage getStartRecord(){
        ElectricyRecords electricyRecords = electricyRecordsService.getStartRecord();
        return RestBuilders.successBuilder(electricyRecords).build();
    }

    /**
     *
     * @param electricyRecordsVo
     * {
     *     deviceCode: //电表设备code
     *     dailyOrMonthly:0 0:逐日,1:逐月 默认0
     *     dailyOrMonthlyDate: 选中月格式:2024-02,选中某天格式:2024-02-03
     * }
     * @return
     */
  @PostMapping(value = "/electricyByDayAndHour")
  public RestMessage  electricyByDayAndHour(@RequestBody ElectricyRecordsVo electricyRecordsVo){
      if (Objects.isNull(electricyRecordsVo.getDailyOrMonthly())){
          electricyRecordsVo.setDailyOrMonthly(0);
      }
      if (StringUtils.isEmpty(electricyRecordsVo.getDailyOrMonthlyDate())){
          electricyRecordsVo.setDailyOrMonthlyDate(DateUtil.getYYYYMMDDDate(new Date()));
      }
      Map<String,Object> result = electricyRecordsService.electricyByDayAndHour(electricyRecordsVo);
      return RestBuilders.successBuilder(result).build();
  }

}
